$(function () {
    findAll();
    findById();
})

//得到菜系
function findAll() {
    let data = myAjax("/back/cuisine/findAll", {}, "get");
    let html = '';
    console.log(data);
    data = data.list;
    if (data != null) {
        for (let i = 0; i < data.length; i++) {
            html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
        }
    }
    $("#cuisine").html(html);
}


function findById() {
    let id = localStorage.getItem("id");
    let data = myAjax("/back/product/findById", {id: id}, "get");
    setProductData(data.productModel);
}

function setProductData(data) {
    // $("input[name=sex][value=" + data.sex + "]").attr("checked", true);
    $("#cuisine").val(data.cuisineId);
    $("#cuisine option[value=" + data.cuisineId + "]").attr("selected", true);
    $("#productName").val(data.productName);
    $("#normalPrice").val(data.normalPrice);
    $("#price").val(data.price);
    $("#content").val(data.content);
    $("input[name=isShow][value=" + data.isShow + "]").attr("checked", true);
    $("input[name=isRecommend][value=" + data.isRecommend + "]").attr("checked", true);
    $("#href").attr("src", data.imgHref);
}

function save() {
    let data = new FormData();

    data.append("cuisine", $("#cuisine").val());
    data.append("productName", $("#productName").val());
    data.append("normalPrice", $("#normalPrice").val());
    data.append("price", $("#price").val());
    data.append("content", $("#content").val());
    data.append("isShow", $("input[name='isShow']:checked").val());
    data.append("isRecommend", $("input[name='isRecommend']:checked").val());
    data.append("imgHref", $("#imgHref")[0].files[0]);
    data.append("id",localStorage.getItem("id"));
    // let result=myAjax("/back/product/add",data,'post');
    $.ajax({
        url: "/back/product/update",//链接后台的地址：对应webservlet里面的值
        data: data,//前端传递给后台的值
        type: "post",//对应servlet里面的doget或者dopost
        async: false,
        processData: false,
        contentType: false,
        dataType: 'json',//统一从后台传递给前端格式
        success: function (data) {//后台传递给前端的值
            if (data.count == 1) {
                $('#right').load('/back/product/productFindAll.html')
            } else {
                alert("修改产品失败");
            }
        }
    })

}